home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSFCOPY.C < prev    next >
C/C++ Source or Header  |  1995-08-04  |  6KB  |  193 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bsfcopy.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains .
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <bs.h>
  45.  
  46.  
  47. //----------------------------------------------------------------------------
  48. //   Description:    Copy a file
  49. //    Parameters:    pcszSrc        Name of source file.
  50. //                        pcszDst        Name of destination file.
  51. //                        fs                Copy options
  52. //                                            FCOPY_NO_NEWER            Don't overwrite newer files
  53. //                                            FCOPY_BACKUP            Create backup
  54. //                                            FCOPY_NO_DATE            Don't preserve date.
  55. //                                            FCOPY_NO_OVERWRITE    Don't overwrite existing.
  56. //                                            FCOPY_APPEND            Append to existing
  57. //                        pusAction    If successful, one or more of the following.
  58. //                                            FCOPY_NEWER_EXISTS
  59. //                                            FCOPY_BACKUP_CREATED
  60. //                                            FCOPY_DST_EXISTS
  61. //                                            FCOPY_FILE_COPIED
  62. //       Returns:    TRUE if successful.
  63. //----------------------------------------------------------------------------
  64. BOOL FN_E CopyFile(PCSZ pcszSrc, PCSZ pcszDst, FLAG16 fs, PUSHORT pusAction)
  65. {
  66.     CHAR szSrc[MAX_PATH];
  67.     CHAR szDst[MAX_PATH];
  68.     HF    hfSrc = -1, hfDst = -1;
  69.     FLAG16 fsSrc = FL_OPEN|FL_READONLY|FL_DENYREADWRITE|FL_BINARY;
  70.     FLAG16 fsDst;
  71.     BOOL fResult = FALSE;
  72.     USHORT usAction = 0;
  73.     BS_FINFO finfoSrc, finfoDst;
  74.     FPOS fposDst;
  75.  
  76.     Assert(pcszSrc && pcszDst);
  77.     strcpy(szSrc, pcszSrc);
  78.     strcpy(szDst, pcszDst);
  79.     FnameNormalize(szSrc, FNAME_DIR);
  80.     FnameNormalize(szDst, FNAME_DIR);
  81.  
  82.     if (!FnameIsFile(szSrc))
  83.         {
  84.         Error("Could not copy from file.\n%s", szSrc);
  85.         goto ERROR_EXIT;
  86.         }
  87.     if (!FinfoGet(szSrc, &finfoSrc))
  88.         goto ERROR_EXIT;
  89.     if (FnameIsFile(szDst))
  90.         {
  91.         if (!FinfoGet(szDst, &finfoDst))
  92.             goto ERROR_EXIT;
  93.  
  94.         usAction |= FCOPY_DST_EXISTS;
  95.         if (fs & FCOPY_NO_OVERWRITE)        // Don't overwrite existing files 
  96.             {
  97.             fResult = TRUE;
  98.             goto ERROR_EXIT;
  99.             }
  100.         if (finfoSrc.timet <= finfoDst.timet)        // Check if existing file is  
  101.             {                                                    //  more current 
  102.             usAction |= FCOPY_NEWER_EXISTS;
  103.             if (fs & FCOPY_NO_NEWER)            
  104.              {
  105.                 fResult = TRUE;
  106.                 goto ERROR_EXIT;
  107.             }
  108.             }
  109.       if (fs & FCOPY_BACKUP)
  110.             {
  111.             CHAR szBackup[MAX_PATH];
  112.  
  113.             FnameAppendExt(strcpy(szBackup, szDst), ".BAK", TRUE);
  114.             if (FnameExists(szBackup))
  115.                 if (!FnameDelete(szBackup))
  116.                     goto ERROR_EXIT;
  117.             if (!FnameRename(szDst, szBackup))
  118.                 goto ERROR_EXIT;
  119.             usAction |= FCOPY_BACKUP_CREATED;
  120.             }
  121.         }
  122.     if (!FileOpen(&hfSrc, szSrc, fsSrc, NULL))
  123.         goto ERROR_EXIT;
  124.  
  125.     if (fs & FCOPY_APPEND)
  126.         fsDst = FL_OPEN|FL_CREATE|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
  127.     else
  128.         fsDst = FL_CREATE|FL_TRUNCATE|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
  129.  
  130.     if (!FileOpen(&hfDst, szDst, fsDst, NULL))
  131.         goto ERROR_EXIT;
  132.  
  133.     if (fs & FCOPY_APPEND)
  134.         fposDst = FileGetSize(hfDst);        // Copy at end of file!!
  135.     else
  136.         fposDst = 0L;
  137.  
  138.     if (!FileCopy(hfDst, fposDst, hfSrc, 0, (FPOS)-1L))
  139.         goto ERROR_EXIT;
  140.  
  141.     FileClose(hfDst);
  142.     hfDst = -1;
  143.     if (!(fs & FCOPY_NO_DATE))                // Set date/time for copied file
  144.         if (!FinfoSet(szDst, &finfoSrc))
  145.             goto ERROR_EXIT;
  146.  
  147.     if (BTEST(fs, FCOPY_RW))
  148.         {
  149.         if (!FinfoGet(szDst, &finfoDst))
  150.             goto ERROR_EXIT;
  151.  
  152.         finfoDst.usAttrib &= ~FA_READONLY;
  153.  
  154.         if (!FinfoSet(szDst, &finfoDst))
  155.             goto ERROR_EXIT;
  156.         }
  157.     usAction |= FCOPY_FILE_COPIED;
  158.     fResult = TRUE;
  159. ERROR_EXIT:
  160.     if (pusAction)
  161.         pusAction[0] = usAction;
  162.     if (hfSrc >= 0)
  163.         FileClose(hfSrc);
  164.     if (hfDst >= 0)
  165.         FileClose(hfDst);
  166.     return fResult;
  167. }
  168.  
  169.  
  170. //----------------------------------------------------------------------------
  171. //   Description:    Run standard test suite
  172. //    Parameters:    sTest        Test to run.
  173. //                                        0        Run all default tests (except).
  174. //       Returns:    TRUE if successful.
  175. //----------------------------------------------------------------------------
  176. #if COMPILE_TEST
  177. BOOL FN CopyTest(SHORT sTest)
  178. {
  179.     NOTUSED(sTest);
  180.  
  181.     if (!CopyFile("\\dev\\base\\bsfcopy.c", "\\dev\\base\\test.tmp", FCOPY_BACKUP, NULL))
  182.         return FALSE;
  183.     if (!CopyFile("\\dev\\base\\bsfcopy.c", "\\dev\\base\\test.tmp", FCOPY_BACKUP, NULL))
  184.         return FALSE;
  185.     if (!CopyFile("\\dev\\base\\bsfcopy.c", "\\dev\\base\\test.tmp", FCOPY_BACKUP|FCOPY_APPEND, NULL))
  186.         return FALSE;
  187.     return TRUE;
  188. }
  189. #endif
  190. //----------------------------------------------------------------------------
  191. //------------------------------- End of File --------------------------------
  192. //----------------------------------------------------------------------------
  193.